Use dfClient.pkg
Use DataDict.pkg
Use dfTable.pkg
Use Windows.pkg
Use File_dlg.Pkg

Use POOCH.DD

Deferred_View Activate_oPoochesGrid for ;
;
Object oPoochesGrid is a dbView

    Property string psMBLabel "Pooches"

    Set Border_Style to Border_Thick
    Set Label to "Pooches Grid View"
    Set Location to 8 17

    Set Size to 247 461

    Object Pooch_DD is a Pooch_DataDictionary
    End_Object    // Pooch_DD

    Set Main_DD to Pooch_DD
    Set Server to Pooch_DD

    Object oPooch_Grid is a dbGrid
        Set Main_File to Pooch.File_Number
        Set Ordering to 1
        Set Size to 162 437
        Set Location to 9 10
        Set peAnchors to anAll
        Set Wrap_State to TRUE

        Begin_Row
            Entry_Item Pooch.Name
            Entry_Item Pooch.Breed
            Entry_Item Pooch.Age
            Entry_Item Pooch.Create_date
            Entry_Item Pooch.Create_time
            Entry_Item Pooch.Change_date
            Entry_Item Pooch.Change_time
        End_Row

        Set Form_Width   item 0 to 62
        Set Header_Label item 0 to "Name"

        Set Form_Width   item 1 to 100
        Set Header_Label item 1 to "Breed"

        Set Form_Width   item 2 to 40
        Set Header_Label item 2 to "Age"

        Set Form_Width   item 3 to 50
        Set Header_Label item 3 to "Create Date"

        Set Form_Width   item 4 to 58
        Set Header_Label item 4 to "Create Time"

        Set Form_Width   item 5 to 60
        Set Header_Label item 5 to "Change Date"

        Set Form_Width   item 6 to 60
        Set Header_Label item 6 to "Change Time"
        Set peResizeColumn to rcAll

    End_Object    // oPooch_Grid

    Object oDeleteBtn is a Button
        Set Label to "Delete All"
        Set Location to 196 35
        Set peAnchors to anBottomLeft

        Procedure OnClick
            Boolean bStop
            Clear Pooch
            Lock
                Repeat
                    Find gt Pooch by index.1
                    If (found) Delete Pooch
                    Else Move True to bStop
                Until (bStop)
            Unlock
            Send Beginning_of_Data to oPooch_Grid
        End_Procedure // OnClick

    End_Object    // oDeleteBtn

    Object oImportGrp is a Group
        Set Size to 62 268
        Set Location to 177 125
        Set peAnchors to anBottomLeftRight
        Set Label to "Import"

        Object oOpenImport is a OpenDialog
            Set Filter_String To 'Comma Separated (*.csv)|*.csv'
            Set Initial_Folder to (psDataPath(phoWorkspace(ghoApplication)))
            Set Dialog_Caption to "Select a file"
        End_Object    // oOpenImport

        Object oInputPath is a Form
            Set Label to "Input File (.csv)"
            Set Size to 13 198
            Set Location to 20 4
            Set peAnchors to anLeftRight
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to jMode_Top
        End_Object    // oInputPath

        Object oBrowse is a Button
            Set Label to "Browse"
            Set Location to 20 210
            Set peAnchors to anBottomRight

            Procedure OnClick
                String sPath
                Boolean bOk
                Get Show_Dialog of oOpenImport To bOk
                If (bOk) Begin
                    Get File_Name of oOpenImport to sPath
                    Set value of oInputPath to sPath
                End
            End_Procedure // OnClick

        End_Object    // oBrowse

        Object oImport_Btn is a Button
            Set Label to "Import"
            Set Location to 42 211
            Set peAnchors to anBottomRight

            Procedure OnClick
                String sPath
                Get Value of oInputPath to sPath
                Send DoImport sPath
                Send Beginning_of_Data to oPooch_Grid
            End_Procedure // OnClick

        End_Object    // oImport_Btn

    End_Object    // oImportGrp

    Procedure DoImport String sPath
        String sName sBreed
        Handle hoDD
        Integer iAge iCh
        
        Boolean bFail
        If (sPath = "") Begin
            Send stop_box "Please enter an input path with file name." "Pooches"
            Procedure_Return
        End
        Move Pooch_DD to hoDD

        Get Seq_New_Channel to iCh
        Direct_Input channel iCh sPath
        If (not(SeqEof)) Begin                                          
            While (not(Seqeof))
                Read channel iCh sName sBreed iAge
                Readln channel iCh                                        // read in the end of line character
                If (sName <> "") Begin
                    Send Clear of hoDD
                    Move sName to Pooch.Name
                    Find eq Pooch by Index.1
                    If (not(Found)) Begin
                        Set Field_Changed_Value of hoDD Field Pooch.Name to sName
                        Set Field_Changed_Value of hoDD Field Pooch.Breed to sBreed
                        Set Field_Changed_Value of hoDD Field Pooch.Age to iAge
                        Get Request_Validate of hoDD to bFail
                        If (not(bFail)) Send Request_Save of hoDD
                         Else send Clear of hoDD
                    End
                End
            Loop
        End
         Else Send Stop_Box ('Input File "' + sPath + '" does not exist.') "Pooches"
        Close_Input channel iCh
        Send Seq_Release_Channel iCh
    End_Procedure

CD_End_Object    // oPoochesAdvanced
